//
//  LGPhotosView.m
//  微博
//
//  Created by MS on 15-12-23.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGPhotosView.h"
#import "LGPhoto.h"
#import "UIImageView+WebCache.h"
#import "MJPhoto.h"
#import "MJPhotoBrowser.h"
#import "LGPhotoView.h"
@implementation LGPhotosView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
    
        [self setUpAllsubView];
        
    }
    return self;
}

-(void) setUpAllsubView
{
    for (int i=0; i<9; i++) {
        
        LGPhotoView * imageV = [[LGPhotoView alloc] init];
        [self addSubview:imageV];
        imageV.tag = i;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [imageV addGestureRecognizer:tap];
        
    }
    
}

-(void)tapClick:(UITapGestureRecognizer *)tap
{
    UIImageView * imagV = (UIImageView *) tap.view;
    
    int i = 0 ;
    NSMutableArray * arr = [NSMutableArray array];
    for (LGPhoto * lgPhoto in _pic_urls) {
        MJPhoto * photo = [[MJPhoto alloc] init];
       
        NSString * photoUrl =lgPhoto.thumbnail_pic.absoluteString;
        photoUrl = [photoUrl stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        photo.url = [NSURL URLWithString:photoUrl];
        
        photo.srcImageView = imagV;
        photo.index =i;
        i++;
        [arr addObject:photo];
    }

    //弹出图片浏览器
    //创建浏览器对象
    MJPhotoBrowser * photoBrowser = [[MJPhotoBrowser alloc] init];
    photoBrowser.currentPhotoIndex = imagV.tag;
    photoBrowser.photos = arr;
    [photoBrowser show];
    
    
}

-(void)setPic_urls:(NSArray *)pic_urls
{
    _pic_urls = pic_urls;
    for (int i = 0; i<self.subviews.count; i++) {
        
        LGPhotoView * imageV =self.subviews[i];
        
        if (i<_pic_urls.count) {
            
            LGPhoto * photo = pic_urls[i];
            imageV.photo =photo;
            imageV.hidden = NO;
        }
        else{
            imageV.hidden = YES;
        }
    
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
   
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat wh = (LGScreenW-2*LGPhotoMargin-2*LGStatusCellMargin)/3;
    int rols = _pic_urls.count==4? 2:3;
    
    for (int i = 0; i<_pic_urls.count; i++) {

        UIImageView * imageV = self.subviews[i];
        int rol = i%rols; //列
        int col = i/rols; //行
        
        x =(wh + LGPhotoMargin)*rol;
        y =(wh + LGPhotoMargin)*col;
        
        imageV.frame = CGRectMake(x, y, wh, wh);

    }

}
@end
